package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0110_IsBalanced
 * Description:
 * Author: xyf
 * Date: 9/7/22 10:50 PM
 * Version: 1.0
 **/
public class P0110_IsBalanced
{
    private boolean res = true;

    public boolean isBalanced(TreeNode root)
    {
        dfs(root);
        return res;
    }

    private int dfs(TreeNode root)
    {
        if (root == null)
        {
            return 0;
        }

        int left = dfs(root.left) + 1;

        int right = dfs(root.right) + 1;

        if (Math.abs(left - right) > 1)
        {
            res = false;
        }
        return Math.max(left, right);
    }
}